package nl.weeaboo.sh.model;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import timon.common.StringUtil;
import timon.common.xml.XmlElement;

import nl.weeaboo.sh.io.CharaImageLoader;

public class CharaImageModel {

	public String id;	
	public Rectangle imageBounds;
	public Point anchor;
	public List<Rectangle>  hitRects;
	public List<DamageRect> damageRects;
	public List<DamageRect> splashRects;
	public List<ColCircle>  colCircles;
	
	public CharaImageModel() {
		id = StringUtil.getRandomId();
		imageBounds = new Rectangle();
		anchor = new Point();
		
		hitRects = new ArrayList<Rectangle>(1);
		damageRects = new ArrayList<DamageRect>(1);
		splashRects = new ArrayList<DamageRect>(1);
		colCircles = new ArrayList<ColCircle>(1);
	}
	public CharaImageModel(CharaImageModel i) {
		this();
		
		imageBounds = new Rectangle(i.imageBounds);
		anchor = new Point(i.anchor);
		
		hitRects.clear();
		for (Rectangle r : i.hitRects) {
			hitRects.add(new Rectangle(r));
		}
		damageRects.clear();
		for (DamageRect r : i.damageRects) {
			damageRects.add(new DamageRect(r));
		}
		splashRects.clear();
		for (DamageRect r : i.splashRects) {
			splashRects.add(new DamageRect(r));
		}
		colCircles.clear();
		for (ColCircle c : i.colCircles) {
			colCircles.add(new ColCircle(c));
		}
	}
	
	//Functions
	public CharaImageModel clone() { return new CharaImageModel(this); }
	
	//Getters
	
	//Setters

	//Save Support
	public XmlElement save() {
		XmlElement e = new XmlElement("image");
		
		e.addChild("id", id);
		CharaImageLoader.saveRect(e, "uv", imageBounds);
		
		XmlElement anchorE = e.addChild("anchor");
		anchorE.addChild("x", anchor.x);
		anchorE.addChild("y", anchor.y);
		
		XmlElement hitRectsE = e.addChild("hitRects");
		for (Rectangle r : hitRects) {
			if (r.width == 0 || r.height == 0) continue;
			CharaImageLoader.saveRect(hitRectsE, "rect", r);
		}
		
		XmlElement damageRectsE = e.addChild("damageRects");
		for (DamageRect r : damageRects) {
			if (r.width == 0 || r.height == 0) continue;
			CharaImageLoader.saveDamageRect(damageRectsE, "rect", r);
		}
		
		XmlElement splashRectsE = e.addChild("splashRects");
		for (DamageRect r : splashRects) {
			if (r.width == 0 || r.height == 0) continue;
			CharaImageLoader.saveDamageRect(splashRectsE, "rect", r);
		}
		
		XmlElement colCirclesE = e.addChild("colCircles");
		for (ColCircle c : colCircles) {
			if (c.getRadius() == 0) continue;
			CharaImageLoader.saveColCircle(colCirclesE, "colCircle", c);
		}
		
		return e;
	}
	
}
